package cn.iocoder.yudao.module.handguard.controller.admin.userdb;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.handguard.controller.admin.client.vo.UserAllocationReqVo;
import cn.iocoder.yudao.module.handguard.controller.admin.order.vo.MemberUserBaseVO;
import cn.iocoder.yudao.module.handguard.controller.admin.userdb.vo.UserInfoDbVO;
import cn.iocoder.yudao.module.handguard.controller.app.orderprocessrecord.vo.OrderProcessRecordCreateReqVO;
import cn.iocoder.yudao.module.handguard.controller.app.orderprocessrecord.vo.OrderProcessRecordFinishReqVO;
import cn.iocoder.yudao.module.handguard.controller.app.orderprocessrecord.vo.OrderProcessRecordRespVO;
import cn.iocoder.yudao.module.handguard.controller.app.processrecord.vo.ProcessRecordRespVO;
import cn.iocoder.yudao.module.handguard.controller.app.user.vo.TransferWarrantReqVo;
import cn.iocoder.yudao.module.handguard.controller.app.user.vo.UpdateIdCardReqVO;
import cn.iocoder.yudao.module.handguard.controller.app.user.vo.UserBaseVO;
import cn.iocoder.yudao.module.handguard.controller.app.usertablesaudit.vo.UserTablesAuditCreateReqsVO;
import cn.iocoder.yudao.module.handguard.controller.app.usertablesaudit.vo.UserTablesAuditUpdateReqVO;
import cn.iocoder.yudao.module.handguard.controller.app.usertablesaudit.vo.UserTablesAuditUpdateReqsVO;
import cn.iocoder.yudao.module.handguard.convert.orderprocessrecord.OrderProcessRecordConvert;
import cn.iocoder.yudao.module.handguard.convert.processrecord.ProcessRecordConvert;
import cn.iocoder.yudao.module.handguard.convert.user.MiniUserConvert;
import cn.iocoder.yudao.module.handguard.dal.dataobject.orderprocessrecord.OrderProcessRecordDO;
import cn.iocoder.yudao.module.handguard.dal.dataobject.processrecord.ProcessRecordDO;
import cn.iocoder.yudao.module.handguard.dal.dataobject.user.UserDO;
import cn.iocoder.yudao.module.handguard.dal.dataobject.usertablesaudit.UserTablesAuditDO;
import cn.iocoder.yudao.module.handguard.dal.mysql.user.MiniUserMapper;
import cn.iocoder.yudao.module.handguard.service.order.OrderService;
import cn.iocoder.yudao.module.handguard.service.orderprocessrecord.OrderProcessRecordService;
import cn.iocoder.yudao.module.handguard.service.processrecord.ProcessRecordService;
import cn.iocoder.yudao.module.handguard.service.user.MiniUserService;
import cn.iocoder.yudao.module.handguard.service.usertablesaudit.UserTablesAuditService;
import cn.iocoder.yudao.module.system.api.notify.NotifyMessageSendApi;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

/**
 * @author: liuweijun
 * @since: 2023/10/9 14:16
 * @description:
 */

@Tag(name = "管理后台 - 用户操作")
@RestController
@RequestMapping("/hutai/userdb")
@Validated
public class UserDbController {

    @Resource
    private MiniUserService miniUserService;

    @Resource
    private MiniUserMapper miniUserMapper;

    @Resource
    private UserTablesAuditService userTablesAuditService;

    @Resource
    private ProcessRecordService processRecordService;

    @Resource
    private OrderProcessRecordService orderProcessRecordService;

    @Resource
    private OrderService orderService;

    @PostMapping("/updateUser")
    @Operation(summary = "修改用户信息")
    @PreAuthorize("@ss.hasPermission('db:user:update')")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody UserInfoDbVO reqVO) {
        miniUserService.dbUpdateUser(reqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除用户")
    @PreAuthorize("@ss.hasPermission('db:user:delete')")
    public CommonResult<Boolean> deleteUser(@RequestParam("id") Long id) {
        miniUserService.deleteUser(id);
        return success(true);
    }


    @PostMapping("/updateIsWork")
    @Operation(summary = "业务员-离职")
    @PreAuthorize("@ss.hasPermission('db:user:delete')")
    public CommonResult<Boolean> updateIsWork(@RequestParam("id") Long id) {
        miniUserService.updateIsWork(id);
        return success(true);
    }


    @GetMapping("/getUser")
    @Operation(summary = "获取当前用户")
    public CommonResult<MemberUserBaseVO> getUser() {
        Long userId = getLoginUser().getId();
        return success(miniUserService.getUserInfo(userId));
    }

    @PostMapping("/forbidden")
    @Operation(summary = "禁用用户")
    @PreAuthorize("@ss.hasPermission('db:user:update')")
    public CommonResult<Boolean> forbiddenUser(@RequestParam("id") Long id) {
        miniUserService.forbiddenUser(id);
        return success(true);
    }

//    @GetMapping("/getUserOneList")
//    @Operation(summary = "用户列表（新增业务员的下拉选项）")
//    public CommonResult<List<UserBaseVO>> getUserList() {
//        LambdaQueryWrapper<UserDO> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(UserDO::getUserType,1);
//        List<UserDO> userDOList = miniUserMapper.selectList(queryWrapper);
//        return success(MiniUserConvert.INSTANCE.convertList(userDOList));
//    }
//
//    @GetMapping("/getUserTwoList")
//    @Operation(summary = "用户列表（新增权证员的下拉选项）")
//    public CommonResult<List<UserBaseVO>> getUser2List() {
//        LambdaQueryWrapper<UserDO> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.in(UserDO::getUserType,1,2);
//        List<UserDO> userDOList = miniUserMapper.selectList(queryWrapper);
//        return success(MiniUserConvert.INSTANCE.convertList(userDOList));
//    }


  // ============== 业务操作

    @GetMapping("/dataList")
    @Operation(summary = "订单详情-完善资料列表")
    @Parameter(name = "id", description = "订单id", required = true, example = "1")
    public CommonResult<Map<String,List<UserTablesAuditDO>>> getOrderDataList(Long id) {
        Map<String,List<UserTablesAuditDO>> listMap = userTablesAuditService.getOrderDataList(id,1);
        return success(listMap);
    }

    @GetMapping("/dataLists")
    @Operation(summary = "订单详情-完善资料列表-驳回后不通过的项")
    @Parameter(name = "id", description = "订单id", required = true, example = "1")
    public CommonResult<Map<String,List<UserTablesAuditDO>>> getOrderDataListTwo(Long id) {
        Map<String,List<UserTablesAuditDO>> listMap = userTablesAuditService.getOrderDataList(id,0);
        return success(listMap);
    }

    @PostMapping("/salesmanCreate")
    @Operation(summary = "业务员-完善资料保存")
    @PreAuthorize("@ss.hasPermission('hutai:userdb:update')")
    public CommonResult<Long> createUserTablesAudit(@Valid @RequestBody UserTablesAuditCreateReqsVO createReqVO) {
        return success(userTablesAuditService.createUserTablesAudits(createReqVO));
    }

    @PostMapping("/salesmanUpdate")
    @Operation(summary = "完善资料驳回-业务员重新提交")
    @PreAuthorize("@ss.hasPermission('hutai:userdb:update')")
    public CommonResult<Boolean> updateUserTablesAudit(@Valid @RequestBody UserTablesAuditUpdateReqsVO updateReqVO) {
        userTablesAuditService.updateUserTablesAudits(updateReqVO);
        return success(true);
    }

    @PostMapping("/data/transfer-warrant")
    @Operation(summary = "业务员-订单转权证")
    @PreAuthorize("@ss.hasPermission('hutai:userdb:update')")
    public CommonResult<String> transferWarrant(@Valid @RequestBody TransferWarrantReqVo reqsVo) {
        //当前用户id
        Long userId = getLoginUser().getId();
        reqsVo.setUserId(userId);
        return miniUserService.transferWarrant(reqsVo);
    }

    @PutMapping("/data/verify")
    @Operation(summary = "权证-资料审核-更新资料记录")
    @PreAuthorize("@ss.hasPermission('warrant:userdb:update')")
    public CommonResult<Boolean> updateUserTablesAudit(@Valid @RequestBody UserTablesAuditUpdateReqVO updateReqVO) {
        userTablesAuditService.updateUserTablesAudit(updateReqVO);
        return success(true);
    }



    @PostMapping("/warrant/create")
    @Operation(summary = "权证-保存受理审核流程记录")
    @PreAuthorize("@ss.hasPermission('warrant:userdb:update')")
    public CommonResult<String> createOrderProcessRecord(@Valid @RequestBody OrderProcessRecordCreateReqVO createReqVO) {
        orderProcessRecordService.createOrderProcessRecord(createReqVO);
        return CommonResult.success("操作成功");
    }

    @PostMapping("/finish")
    @Operation(summary = "权证-受理审核流程结单")
    @PreAuthorize("@ss.hasPermission('warrant:userdb:update')")
    public CommonResult<String> finish(@Valid @RequestBody OrderProcessRecordFinishReqVO finishReqVO) {
        orderService.updateOrderStateAndAmount(finishReqVO.getOrderId(), finishReqVO.getRealAmount());
        return CommonResult.success("操作成功");
    }

    @GetMapping("/list/by/order")
    @Operation(summary = "权证-根据订单ID查询审核流程记录")
    @Parameter(name = "orderId", description = "订单编号", required = true, example = "1024")
    public CommonResult<List<ProcessRecordRespVO>> getOrderProcessRecordList(@RequestParam("orderId") Long orderId) {
        List<ProcessRecordDO> list = processRecordService.getProcessRecordList(orderId);
        return success(ProcessRecordConvert.INSTANCE.convertList(list));
    }


    @PostMapping("/update/idCard")
    @Operation(summary = "业务员-完善身份证号")
    //@PreAuthorize("@ss.hasPermission('hutai:userdb:updateidard')")
    public CommonResult<Boolean> updateIdCard(@Valid @RequestBody UpdateIdCardReqVO reqVo) {
        //当前用户id
        Long userId = getLoginUser().getId();
        return success(miniUserService.updateIdCard(userId,reqVo));
    }


}
